<template>
  <div>
    <el-dialog title="选择采购订单" :visible.sync="selectIndentVisible">
      <el-table
              :data="curIndents"
              border
              style="width: 100%"
              @current-change="indentClickChange"
      >
        <el-table-column label="选择" width="55">
          <template slot-scope="scope">
            <el-radio v-model="selectIndentRadio" :label="scope.row"><i /></el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="indentCode" label="采购订单号" align="center" width="180px">
          <template slot-scope="scope"><span>{{ scope.row.indentCode }}</span></template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="订单总金额" align="center" width="160px">
          <template slot-scope="scope"><span>{{ scope.row.totalPrice }}</span></template>
        </el-table-column>
        <el-table-column prop="projectComment" label="备注" align="center">
          <template slot-scope="scope"><span>{{ scope.row.projectComment }}</span></template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" @click="selectIndentVisible = false">取消</el-button>
        <el-button icon="el-icon-check" type="primary" @click="exportWithIndent">确定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { getFileAction, getAction } from '@/api/manage'
import { Message } from 'element-ui'

export default {
  name: '',
  components: {
  },
  props: {
  },
  data() {
    return {
      curIndents: [],
      selectIndentVisible: false,
      selectCode: '',
      selectIndentRadio: null
    }
  },
  created() {
  },
  methods: {
    btnIndent(row) {
      getAction('/purchase/contract/queryIndents', { contractId: row.id }).then(res => {
        const { data } = res
        if (data !== undefined && data != null) {
          if (data.length > 1) {
            // 弹出选择
            this.selectIndentRadio = null
            this.selectIndentVisible = true
            this.curIndents = data
            this.selectCode = row.contractCode
          } else {
            this.exportIndent({ contractId: row.id, indentId: data[0].id }, row.contractCode)
          }
        }
      })
    },
    exportIndent(param, indentCode) {
      getFileAction('/purchase/contract/exportIndent', param).then(res => {
        if (res.type === 'application/json') {
          const reader = new FileReader() // 创建读取文件对象
          reader.addEventListener('loadend', function() { //
            const resJson = JSON.parse(reader.result) // 返回的数据
            Message.error(resJson.msg)
          })
          reader.readAsText(res, 'utf-8') // 设置读取的数据以及返回的数据类型为utf-8
        } else {
          const doca = window.document.createElement('a')
          const url = window.URL.createObjectURL(res)

          doca.href = url
          doca.setAttribute('download', '采购订单' + indentCode + '.xls')
          window.document.body.appendChild(doca)
          doca.click()
          window.document.body.removeChild(doca)
          window.URL.revokeObjectURL(url)
        }
      })
    },
    exportWithIndent() {
      if (this.selectIndentRadio === null) {
        Message.error('请选择要导出的采购订单')
      } else {
        this.exportIndent({ contractId: this.selectIndentRadio.contractId, indentId: this.selectIndentRadio.id }, this.selectCode)
        this.selectIndentVisible = false
      }
    },
    indentClickChange(item) {
      this.selectIndentRadio = item
    }

  }
}
</script>
